﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Bit.Art.Core.Models
{
    /// <summary>
    /// 撮合交易订单详情
    /// </summary>
    public class ExchangeOrderInfo : IEqualityComparer<ExchangeOrderInfo>
    {
        /// <summary>
        ///  订单Id
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 交易对
        /// </summary>
        public string Market { get; set; }

        /// <summary>
        /// 下单数量
        /// </summary>
        public decimal Quantity { get; set; }

        /// <summary>
        /// 未成交数量
        /// </summary>
        public decimal QuantityRemaining { get; set; }

        /// <summary>
        /// 下单价格
        /// </summary>
        public decimal? Price { get; set; }

        /// <summary>
        /// 下单时间
        /// </summary>
        public DateTime PlacedTimestamp { get; set; }

        /// <summary>
        /// 完全撮合时间
        /// </summary>
        public DateTime? ClosedTimestamp { get; set; }

        /// <summary>
        /// 交易方向
        /// </summary>
        public TradeSide Side { get; set; }

        /// <summary>
        /// 手续费，以交易对（<see cref="Quote"/>）计量
        /// </summary>
        public decimal? Commission { get; set; }
        
        /// <summary>
        /// 撮合情况
        /// </summary>
        public OrderFillType FillType
        {
            get
            {
                if (QuantityRemaining == 0M)
                    return OrderFillType.Filled;
                else if (QuantityRemaining > 0M && QuantityRemaining < Quantity)
                    return OrderFillType.PartialFilled;
                else
                    return OrderFillType.NotFilled;
            }
        }

        /// <summary>
        /// 对比两个订单是否相同
        /// </summary>
        /// <param name="x">订单x</param>
        /// <param name="y">订单y</param>
        /// <returns>如果交易对、价格、交易方向和数量都相同，则认为相同</returns>
        public bool Equals(ExchangeOrderInfo x, ExchangeOrderInfo y)
        {
            if (x == null && y == null) return true;
            else if (x == null && y != null) return false;
            else if (x != null && y == null) return false;
            else
            {
                return string.Compare(x.Market, y.Market, true) == 0 &&
                       x.Side == y.Side &&
                       x.Quantity == y.Quantity &&
                       x.Price == y.Price;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int GetHashCode(ExchangeOrderInfo obj)
        {
            return obj.GetHashCode();
        }
    }
}
